package com.starbaba.core.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.starbaba.core.hibernate.MatchType;
import com.starbaba.core.hibernate.PropertyType;
import com.starbaba.core.web.constant.WebConstants;


@SuppressWarnings({"serial","unused"})
public class PropertyParamMapBuilder implements Serializable{
	
	private Log log = LogFactory.getLog(getClass());
	
	public PropertyParamMapBuilder() {
	}
	
	/**
	 * 从HttpRequest中创建PropertyParamMap列表, 默认PropertyPrefix属性名前缀为"_".
	 * 
	 * @see #buildFromHttpRequest(HttpServletRequest, String)
	 */
	public static Map<String, Object> buildFromHttpRequest(final HttpServletRequest request, Map<String, Object> paramsMap) {
		return buildFromHttpRequest(request, WebConstants.PROPERTY_PREFIX, paramsMap);
	}


	/**
	 * 从HttpRequest中创建ParamsMap列表
	 * eg.
	 * _deq_age
	 * _slike_name
	 * _sin_xx
	 */
	public static Map<String, Object> buildFromHttpRequest(final HttpServletRequest request, final String propertyPrefix, Map<String, Object> paramsMap) {
		if(null == paramsMap){
			paramsMap = new HashMap<String, Object>();
		}else{
			paramsMap.clear();
		}
		
		//从request中获取含属性前缀名的参数
		Map<String, Object> pageParamsMap = ServletUtils.getParametersAll(request);
		//分析参数Map,构造paramsMap列表
		for (Map.Entry<String, Object> entry : pageParamsMap.entrySet()) {
			String pagePropertyName = entry.getKey();//页面属性名
			Object pagePropertyValue = entry.getValue();//页面属性值
//			log.info(pagePropertyName+"--------"+pagePropertyValue);
			if(pagePropertyValue instanceof java.lang.String[]){
				String []pagePropertyValues = getEffectiveValue(pagePropertyValue);
				if(null != pagePropertyValues && pagePropertyValues.length > 0){
					paramsMap.put(pagePropertyName, pagePropertyValues);
				}
			}else{
				if(StringUtils.isNotBlank((String)pagePropertyValue)){
					paramsMap.put(pagePropertyName, String.valueOf(pagePropertyValue).trim());
				}
			}
		}
		return paramsMap;
	}
	
	/**
	 * @param propertyPrefix 	页面属性前缀
	 * @param pagePropertyName 	页面属性名称
	 * @param pagePropertyValue 页面属性值
	 *                   eg. seq_name
	 * @param value 待比较的值.
	 */
	public static Object buildPropertyValue(final String propertyPrefix, final String pagePropertyName, final Object pagePropertyValue) {
		String pagePropertyNameWithOutPrefix = pagePropertyName.substring(propertyPrefix.length());		
		String firstPart = StringUtils.substringBefore(pagePropertyNameWithOutPrefix, "_");//第一部分为类型, seq
		String propertyTypeCode = StringUtils.substring(firstPart, 0, 1);//取出属性类型, s
		String matchTypeCode = StringUtils.substring(firstPart, 1);//取出比较类型, eq

		Class<?> propertyClass = null;
		
		try {
			propertyClass = Enum.valueOf(PropertyType.class, propertyTypeCode).getValue();
		} catch (RuntimeException e) {
			throw new IllegalArgumentException("属性类型标识" + pagePropertyName + "没有按规则编写,无法得到属性值类型.", e);
		}
		
		try {
			Enum.valueOf(MatchType.class, matchTypeCode);
		} catch (RuntimeException e) {
			throw new IllegalArgumentException("属性比较类型标识" + pagePropertyName + "没有按规则编写,无法得到属性比较类型.", e);
		}

		if(pagePropertyValue instanceof java.lang.String[]){
			String[] propertyValues = (String[])pagePropertyValue;
			Object []values = new Object[propertyValues.length];
			for (int i = 0; i < propertyValues.length; i++) {
				values[i] = ConvertUtils.convertStringToObject(propertyValues[i], propertyClass);
			}
			return values;
		}else{
			return ConvertUtils.convertStringToObject((String)pagePropertyValue, propertyClass);
		}
	}
	
	public static String[] getEffectiveValue(Object pagePropertyValue){
		String []array = (String[])pagePropertyValue;
		List<String> ls = new ArrayList<String>();
		for (int index = 0; index < array.length; index++) {
			if(StringUtils.isNotBlank(array[index])){
				ls.add(array[index].trim());
			}
		}
		String []newls = new String[ls.size()];
		return ls.toArray(newls);
	}
	
}
